package hsit;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author huanggy
 * @date 2022/07/07
 **/
public class Analyze_DZY_Data {


    public static final String ENTERPRISE_IS_NULL = "企业名称为空";

    public static void main(String[] args) {
        // 解析电子烟数据，获取spuid和错误问题
        ExcelReader dzyReader = ExcelUtil.getReader("D:\\Application\\0707\\dzy_data.xlsx");
        List<List<Object>> dzyDataList = dzyReader.read();

        // 移除标题
        dzyDataList.remove(0);

        Map<String, String> dzyMap = new HashMap<>();
        dzyDataList.stream().filter(spu -> spu.get(0) != null && spu.get(1) != null).forEach(spu -> {
            dzyMap.put(spu.get(1).toString().replace("'", "").trim(), spu.get(0).toString().trim());
        });

        // 解析商品中心数据
        Map<String, String> ccBrandMap = new HashMap<>();
        Map<String, String> ccEnterpriseMap = new HashMap<>();
        ExcelReader ccReader = ExcelUtil.getReader("D:\\Application\\0707\\cc_data.xlsx");
        List<List<Object>> ccDataList = ccReader.read();
        ccDataList.forEach(spu -> {
            ccBrandMap.put(spu.get(2).toString().trim(), spu.get(9).toString().trim());
            ccEnterpriseMap.put(spu.get(2).toString().trim(), spu.get(4).toString().trim());
        });

        System.out.println(ccEnterpriseMap.get("1001220701000004633"));

        // 匹配
        List<List<Object>> result = new LinkedList<>();
        dzyMap.keySet().forEach(key -> {
            String dzyErrorType = dzyMap.get(key);
            List<Object> data = new LinkedList<>();
            // spu
            data.add(key);
            data.add(dzyErrorType);
            if (ENTERPRISE_IS_NULL.equals(dzyErrorType)) {
                String ccEnterprise = ccEnterpriseMap.get(key);
                data.add(ccEnterprise);
            } else {
                String ccBrand = ccBrandMap.get(key);
                data.add(ccBrand);
            }
            result.add(data);
        });

        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("D:\\Application\\0707\\result.xlsx");
        //跳过当前行，既第一行，非必须，在此演示用
        writer.passCurrentRow();
        //一次性写出内容，强制输出标题
        writer.write(result, true);
        //关闭writer，释放内存
        writer.close();
    }
}
